---------------------------------------
--           AmBContentGrid          --
---------------------------------------
-- Panel that places controls at
-- different places inside a grid. Cells
-- are defined relative to grid size
local ContentGrid = {
   Init = function(self)
      self._Rows = { } -- Location of row dividers
      self._Cols = { } -- Location of col dividers
      self._Cells  = { } -- Table of cells(mapping tuples containing colstart, rowstart, colend, rowend to a control)

      --Default dividers
      self._Left = self:AddRelativeDivider(false, 0.0)
      self._Right = self:AddRelativeDivider(false, 1.0)
      self._Top = self:AddRelativeDivider(true, 0.0)
      self._Bottom = self:AddRelativeDivider(true, 1.0)
   end,

   -- Adds a divider to the grid. First argument specifies
   -- if it is a row(true) or a coloumn(false). Second argument
   -- is a function which takes(grid width, grid height, relavant dimension) and returns
   -- offset from the top/left part of the grid. This function
   -- returns the index to the divider.
   AddDivider = function(self, IsRow, Location) 
      local seplist = nil
      if IsRow then
         seplist = self._Rows
      else
         seplist = self._Cols
      end
      table.insert(seplist, Location)
      return #seplist
   end,

   -- Adds a divider at an absolute position. Use a
   -- negative number to specify an offset from the
   -- bottom/right.
   AddAbsoluteDivider = function(self, IsRow, Location)
      if Location >= 0 then
         return self:AddDivider(IsRow, function(wide, tall, rel) return Location end)
      else
         return self:AddDivider(IsRow, function(wide, tall, rel) return (rel + Location) end)
      end
   end,

   -- Adds a divider at an relative position(0.0 to 1.0).
   AddRelativeDivider = function(self, IsRow, Location)
      return self:AddDivider(IsRow, function(wide, tall, rel) return Location * rel end)
   end,

   -- Gets the panel for the cell at the location.
   GetCellPanel = function(self, ColStart, RowStart, ColEnd, RowEnd)
      -- Check for existance
      for k, v in pairs(self._Cells) do
         if k[1] == ColStart and k[2] == RowStart and k[3] == ColEnd and k[4] == RowEnd then
            return v
         end
      end

      -- Create new control
      local control = vgui.Create("AmBContentPanel", self)
      self._Cells[{ColStart, RowStart, ColEnd, RowEnd}] = control
      return control
   end,

   -- Default dividers
   GetLeft = function(self) return self._Left end,
   GetRight = function(self) return self._Right end,
   GetTop = function(self) return self._Top end,
   GetBottom = function(self) return self._Bottom end,

   PanelThink = function(self)
      self:_UpdateCells()
   end,

   -- Updates cell positions
   _UpdateCells = function(self)
      width = self:GetWide()
      height = self:GetTall()
      for k, v in pairs(self._Cells) do
         left = self._Cols[k[1]] (width, height, width)
         top = self._Rows[k[2]](width, height, height)
         right = self._Cols[k[3]](width, height, width)
         bottom = self._Rows[k[4]](width, height, height)
         v:SetPos(left, top)
         v:SetSize(right - left, bottom - top)
      end
   end
}
AmB.UI.ContentGrid = ContentGrid
vgui.Register("AmBContentGrid", ContentGrid, "AmBContentPanel")